"""
Case Type   : 资源池化故障模式库
Case Name   : data共享盘无权限
Create At   : 2025/05
Owner       : haomeng
Description :
    1、查看集群状态
    2、运行tpcc业务
    3、修改主节点data共享盘无权限
    4、查看业务运行情况
    5、重启集群,查看集群状态
    6、恢复data共享盘权限
    7、拉起集群，恢复集群状态
Expect      :
    1、集群状态正常
    2、运行业务成功
    3、修改主节点data盘权限成功
    4、业务运行无影响
    5、重启集群失败，原主节点被踢出
    6、data盘权限恢复正常
    7、集群状态恢复正常
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

class SSFauleMode(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = [Node(node='PrimaryRoot'), Node(node='PrimaryDbUser')]
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.vg_conf = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg', 'dss_vg_conf.ini')

        self.log.info('-----获取data共享盘的lun名称-----')
        vg_name = self.user_node[1].sh(
            f"cat {self.vg_conf} | awk 'NR==1' | awk -F ':' '{{{{print $2}}}}'").result()
        self.log.info(vg_name)
        lun_name = self.user_node[1].sh(f"ls -l {vg_name}").result().split('/')[-1]
        self.log.info(lun_name)
        self.data_lun = f'/dev/{lun_name}'

    def test_ss_faule(self):
        text = '-----step1:查看集群状态;  expect:集群状态正常-----'
        self.log.info(text)

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Normal', status, '集群状态异常')

        text = '-----step2:执行tpcc读写业务  expect:业务执行成功-----'
        self.log.info(text)

        tpcc_cmd = f'./runBenchmark.sh {macro.PROPS_FILENAME}'
        self.log.info(tpcc_cmd)

        tpcc_execute = ComThread(self.com.start_tpcc, args=(self.user_node[0],
                                                            f'{macro.TPCC_PATH}',
                                                            tpcc_cmd))
        tpcc_execute.setDaemon(True)
        tpcc_execute.start()

        text = '-----step3:修改主节点data共享盘为无权限访问状态  expect:修改成功-----'
        self.log.info(text)

        chmod = f'chmod 000 {self.data_lun};ls -al {self.data_lun}'
        self.log.info(chmod)

        chmod_execute = ComThread(self.com.get_sh_result, args=(self.user_node[1], chmod))
        chmod_execute.setDaemon(True)
        chmod_execute.start()

        text = '-----step4:查看业务运行情况  expect:业务运行正常无影响-----'
        self.log.info(text)

        tpcc_execute.join()
        ret1 = tpcc_execute.get_result()
        self.log.info(ret1)
        self.assertIn('ERROR', ret1, 'tpcc执行失败')

        chmod_execute.join()
        ret2 = chmod_execute.get_result()
        self.log.info(ret2)
        self.assertIn('b---', ret2, '修改data共享盘权限失败')

        text = '-----step5:重启集群，查看集群状态  expect:重启失败，原主节点被踢出-----'
        self.log.info(text)

        restart = self.pri_sh.exec_cm_ctl('stop;', 'cm_ctl start -t 30')
        self.log.info(restart)
        self.assertIn(self.constant.cm_stop_success_msg, restart, '停止重启失败')
        self.assertIn(self.constant.cm_start_failed_msg, restart, '启动集群成功')

        time.sleep(30)
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertNotIn('cluster_state   : Normal', status, '集群重启失败')
        self.assertIn('P Down', status, '原主节点未踢出')

    def tearDown(self):
        text = '-----step6:恢复data共享盘权限  expect:恢复成功-----'
        self.log.info(text)

        rechmod = f'chmod 660 {self.data_lun};ls -al {self.data_lun}'
        self.log.info(rechmod)
        rechmod_execute = self.user_node[1].sh(rechmod).result()
        self.log.info(rechmod_execute)

        text = '-----step7：拉起被踢出节点，恢复集群初始状态，查看集群状态  expect:拉起节点成功，恢复集群状态成功-----'
        self.log.info(text)

        restart = self.pri_sh.exec_cm_ctl('start')
        self.log.info(restart)

        time.sleep(60)
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)

        if 'P Primary' not in status:
            switch = self.pri_sh.exec_cm_ctl('switchover', '-a')
            self.log.info(switch)
            self.assertIn(self.constant.cm_switchover_success_msg, switch, '切换主节点失败')
        self.assertIn('brw-rw-', rechmod_execute, '修改data共享盘权限失败')
        self.assertIn(self.constant.cm_start_success_msg, restart, '启动集群失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
